package com.primeton.whdc.ccms.reportimpl.controller;

import com.auth0.jwt.JWT;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.primeton.whdc.ccms.reportapi.response.Result;
import com.primeton.whdc.ccms.reportapi.response.ResultGenerator;
import com.primeton.whdc.ccms.reportimpl.param.CapUser;
import com.primeton.whdc.ccms.reportimpl.service.ILoginService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @author: YunTao.Li
 * @create: 2019-12-11 14:26
 * @description:
 **/
@Controller
public class LoginController {

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private ILoginService loginService;

    /**
     * 获得登陆的用户
     *
     * @return : Result
     * @author : YunTao.Li
     */
    @ResponseBody
    @RequestMapping("/login/user")
    @ApiOperation(value = "获得登陆的用户")
    public Result user(@RequestHeader HttpHeaders headers) {
        Map userMap = new HashMap();

        userMap.put("empCode", "10029417");
        userMap.put("empName", "刘佳");
        userMap.put("deptCode",  "KF47021");
        userMap.put("deptName", "人力资源部（党委组织部）");
//        try {
//            String token = headers.get("Authorization").toString();
//            DecodedJWT jwt = JWT.decode(token);
//            String jwtvalue = jwt.getSubject();
//            Gson gson = new GsonBuilder().create();
//            CapUser user = gson.fromJson(jwtvalue, CapUser.class);
//            String cofUser = user.getName();
//
//            Map<String, String> mapUser = loginService.getEmpInfoByCofUser(cofUser);
//
//            userMap.put("empCode", mapUser.get("empCode"));
//            userMap.put("empName", mapUser.get("empName"));
//            userMap.put("deptCode", mapUser.get("deptCode"));
//            userMap.put("deptName", mapUser.get("deptName"));
//            userMap.put("orgCode", mapUser.get("orgCode"));
//            userMap.put("orgName", mapUser.get("orgName"));
//
//        } catch (Throwable e) {
//            e.printStackTrace();
//            return ResultGenerator.genFailedResult(e.getMessage());
//        }

        return ResultGenerator.genOkResult(userMap);
    }

}
